<?php
class SprintsService extends Service{

    
    /**
     * @param int id
     * <pre>
     * {"type":"integer"}
     * </pre>
     * @return void
     */
    public static function delete($id) {
        if (self::_invoke()) return self::_output();
        $sprint = Doctrine::getTable('Sprint')->find($id);
        $sprint->delete();
    }

    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "description":{"type":"string","minLength":5},
     *     "start_date":{"type":"string","format":"date"},
     *     "end_date":{"type":"string","format":"date"}
     *   }
     * }
     * </pre>
     * @return void
     */
    public static function edit($data) {
        if (self::_invoke()) return self::_output();
        $sprint = Doctrine::getTable('Sprint')->find($data->id);
        $sprint->name = $data->name;
        $sprint->description = $data->description;
        $sprint->start_date = $data->start_date;
        $sprint->end_date = $data->end_date;
        $sprint->save();
    }

    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "name":{"type":"string","minLength":5},
     *     "description":{"type":"string","minLength":5},
     *     "start_date":{"type":"string","format":"date"},
     *     "end_date":{"type":"string","format":"date"}
     *   }
     * }
     * </pre>
     * @return int
     */
    public static function insert($data) {
        if (self::_invoke()) return self::_output();
        if(!sfContext::getInstance()->getUser()->hasAttribute('projectId')) {
            throw new NoProjectSelectedException;
        }
        $projectId = sfContext::getInstance()->getUser()->getAttribute('projectId');
        
        $conn = Doctrine_Manager::connection();
        $conn->beginTransaction();
        $sprint = new Sprint;
        $sprint->name = $data->name;
        $sprint->project_id = $projectId;
        $sprint->description = $data->description;
        $sprint->start_date = $data->start_date;
        $sprint->end_date = $data->end_date;
        $sprint->save();
        $conn->commit();
        return $sprint->id;
    }
    
    /**
     * @param int id
     * @return object
     * {
     *   "type":"object",
     *   "properties": {
     *     "name":{"type":"string","minLength":5},
     *     "description":{"type":"string","minLength":5},
     *     "start_date":{"type":"string","format":"date"},
     *     "end_date":{"type":"string","format":"date"}
     *   }
     * }
     */
    public static function get($id) {
        $sprint = Doctrine::getTable('Sprint')->find($id,Doctrine::HYDRATE_ARRAY);
        return $sprint;
    }

    /**
     * @param int id
     * @return object
     * <pre>
     * @return array
     * {
     *   "type":"array",
     *   "items": {
     *     "type":"object",
     *     "properties": {
     *       "name":{"type":"string","minLength":5},
     *       "description":{"type":"string","minLength":5},
     *       "start_date":{"type":"string","format":"date"},
     *       "end_date":{"type":"string","format":"date"}
     *     }
     *   }
     * }
     * </pre>
     */
    public static function getAll() {
        if(!sfContext::getInstance()->getUser()->hasAttribute('projectId')) {
            throw new NoProjectSelectedException;
        }
        $projectId = sfContext::getInstance()->getUser()->getAttribute('projectId');
        $sprints = SprintTable::findAllByProject($projectId);
        return $sprints;
    }

    public static function getSprintTasks($sprintId) {
        $sprint = Doctrine::getTable('Sprint')->find($sprintId, Doctrine::HYDRATE_ARRAY);
        $tasks = DevTaskTable::findByLogPeriodAndStates($sprint['project_id'], $sprint['start_date'], $sprint['end_date']);
        $arTasks = array();
        $sumSprint = 0;
        $sumDone = 0;
        foreach($tasks as $task) {
            $ar = array();
            $ar['id'] = $task['id'];
            $path = DevTaskTable::getPath($task,2);
            $last = array_pop($path);
            $ar['name'] = implode(' > ', $path ).' > <b>'.$last.'</b>';
            $ar['estimated'] = $task['estimated'];
            $ar['sprint_time'] = null;
            $ar['state'] = $task['state'];
            $ar['work_in_progress_time'] = null;
            $ar['done_time'] = null;
            foreach($task['TaskStateLog'] as $log) {
                switch($log['new_state']) {
                    case DevTask::SPRINT:
                        if(!$ar['sprint_time']) {
                            $sumSprint += $ar['estimated'];
                        }
                        $ar['sprint_time'] = $log['time'];
                        break;
                    case DevTask::WORK_IN_PROGRESS:
                        $ar['work_in_progress_time'] = $log['time'];
                        break;
                    case DevTask::DONE:
                        if(!$ar['done_time']) {
                            $sumDone += $ar['estimated'];
                        }
                        $ar['done_time'] = $log['time'];
                        break;
                }
            }
            $arTasks[] = $ar;
        }
        $arRet = array(
            'sprint' => $sprint,
            'tasks' => $arTasks,
            'sumDone' => $sumDone,
            'sumSprint' => $sumSprint,
            'countTasks' => count($arTasks)
        );
        return $arRet;
    }


}